package sword.T32;

import util.TreeNode;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: duyuzhou
 * @Create: 2022-04-07 16:26:54 星期四
 */

class Solution {
    public int[] levelOrder(TreeNode root) {
        List<Integer> arr = new ArrayList<>();
        Deque<TreeNode> dq = new LinkedList<>();
        if (root == null)
            return new int[]{};
        dq.offer(root);
        while (!dq.isEmpty()) {
            int size = dq.size();
            for (int i=0; i<size; ++i) {
                TreeNode front = dq.poll();
                arr.add(front.val);
                if (front.left != null)
                    dq.offer(front.left);
                if (front.right != null)
                    dq.offer(front.right);
            }
        }
        int[] result = new int[arr.size()];
        for (int i=0; i<arr.size(); ++i)
            result[i] = arr.get(i);
        return result;
    }
}